﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.BD.VMTool.Until
{
    class ApiServiceUtil
    {
        private static CPPEI.Net.Original.UDP.UDPClient s_udpClient;
        private static string s_apiServiceIP;
        private static int s_apiServicePort;

        public static void Close()
        {
            if (s_udpClient != null)
            {
                s_udpClient.Dispose();
            }
        }

        /// <summary>
        /// 向服务器发送修改组件属性的数据包
        /// </summary>
        /// <param name="partName">组件名称</param>
        /// <param name="propertyName">属性名称</param>
        /// <param name="xmlString">属性的值</param>
        public static void SendToApiService(string partName, string propertyName, string xmlString)
        {
            if (s_udpClient == null)
            {
                s_udpClient = new Net.Original.UDP.UDPClient();
                s_apiServiceIP = System.Configuration.ConfigurationManager.AppSettings["ApiServiceIP"];
                s_apiServicePort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ApiServicePort"]);
            }

            byte[] buffer = new byte[1024];
            int offset = 0;
            CPPEI.Net.BufferUtil.StringToBuffer(App.Coolzon_Domain, buffer, ref offset, 128);
            CPPEI.Net.BufferUtil.StringToBuffer(App.Coolzon_Solution, buffer, ref offset, 48);
            CPPEI.Net.BufferUtil.StringToBuffer(App.Coolzon_Scene, buffer, ref offset, 48);
            CPPEI.Net.BufferUtil.StringToBuffer(partName, buffer, ref offset, 48);
            CPPEI.Net.BufferUtil.StringToBuffer(propertyName, buffer, ref offset, 48);
            CPPEI.Net.BufferUtil.StringToBuffer(xmlString, buffer, ref offset, 512);

            CPPEI.Net.Original.Package package = new CPPEI.Net.Original.Package(0x01, buffer, offset);
            s_udpClient.SendTo(s_apiServiceIP, s_apiServicePort, package);
        }
    }
}
